package com.fau.wmbj.server.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.fau.wmbj.server.config.CurrentUser;
import com.fau.wmbj.server.entity.Order;
import com.fau.wmbj.server.entity.*;
import com.fau.wmbj.server.service.OrderMemberService;
import com.fau.wmbj.server.service.OrderService;
import com.fau.wmbj.server.service.TemplateService;
import com.fau.wmbj.server.service.UserService;
import com.fau.wmbj.server.utils.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单
 *
 * @author jiawei
 * @version 1.0
 * @datetime 2024-04-15 17:10
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private TemplateService templateService;
    @Resource
    private UserService userService;
    @Resource
    private OrderMemberService orderMemberService;

    /**
     * 根据id查询
     */
    @RequestMapping(value = "/getOne", method = RequestMethod.POST)
    @ResponseBody
    public Message getOne(@RequestBody Order order, @CurrentUser User user) {
        List<Filter> filters = order.getFilters();
        Order order1 = orderService.findEntity(filters);
        Template template1 = templateService.find(order1.getTemplateId());
        template1.setCompany(null);
        order1.setTemplate(template1);
        List<OrderMember> orderMembers = order1.getOrderMembers();
        OrderMember orderMember = null;
        for (OrderMember om : orderMembers) {
            if (om.getUser().equals(user)) {
                orderMember = om;
                break;
            }
        }
        if (orderMember != null) {
            List<String> purview = orderMember.getPurview();
            List<Map<String, Object>> maps = LuckySheet.filterData2(order1.getBody(), template1.getHeaderRow(), purview);
            order1.setBody(maps);
        }
        return Message.success(order1);
    }

    /**
     * 查询用户
     */
    @RequestMapping(value = "/getUsers", method = RequestMethod.POST)
    @ResponseBody
    public Message getUsers(@CurrentUser User user) {
        Company company = user.getRole().getCompany();
        List<User> list = userService.findList(Filter.eq("role.company", company));
        return Message.success(list);
    }

    /**
     * 查询模板
     */
    @RequestMapping(value = "/getCurrentTemplates", method = RequestMethod.POST)
    @ResponseBody
    public Message getCurrentTemplates(@CurrentUser User user) {
        Company company = user.getRole().getCompany();
        List<Template> templates = templateService.findList(Filter.eq("company", company));
        for (Template template : templates) {
            template.setRole((List<Map<String, Object>>) template.getTableData().get("role"));
        }
        return Message.success(templates);
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ResponseBody
    public Message page(@RequestBody Pageable pageable) {
        Page<Order> page = orderService.findPage(pageable);
        List<Order> content = page.getContent();
        for (Order order : content) {
            Map<String, Object> params = order.getParams();
            params.put("memberCount", order.getOrderMembers().size());
            order.setParams(params);
        }
        return Message.success(page);
    }

    /**
     * 删除行
     */
    @RequestMapping("/deleteRow")
    @ResponseBody
    public Message deleteRow(@RequestBody Order order, @CurrentUser User user) {
        Order order1 = orderService.find(order.getId());
        Map<String, Object> params = order.getParams();
        Integer start = (Integer) params.get("start");
        Integer end = (Integer) params.get("end");
        Map<String, Object> sheet = LuckySheet.deleteRow(order1.getBody().get(0), start, end);
        order1.getBody().set(0, sheet);
        orderService.update(order1);
        return Message.success();
    }

    /**
     * 添加
     */
    @RequestMapping("/save")
    @ResponseBody
    public Message save(@RequestBody Order order, @CurrentUser User user) {
        orderService.save(order, user);
        return Message.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public Message update(@RequestBody Order order, @CurrentUser User user) {
        orderService.update2(order, user);
        return Message.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Message delete(@RequestBody Role role) {
        Long[] ids = role.getIds();
        try {
            for (Long id : ids) {
                Order order = orderService.find(id);
                List<OrderMember> orderMembers = order.getOrderMembers();
                for (OrderMember orderMember : orderMembers) {
                    orderMemberService.delete(orderMember.getId());
                }
                orderService.delete(id);
            }
        } catch (Exception e) {
            return Message.error(e.getMessage());
        }
        return Message.success();
    }


    /**
     * 计算
     */
    @RequestMapping("/calculate")
    @ResponseBody
    public Message calculate(@RequestBody Order order, @CurrentUser User user) {
        String val = orderService.calculate(order, user);
        return Message.success(val);
    }
}
